package One;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTxt;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class LoginFrame extends JFrame {
    private LovoTxt userTxt = new LovoTxt("用户名",60,50,this);
    private LovoTxt pwdTxt = new LovoTxt("密码",60,100,this);
    //时间
    private LocalTime now = LocalTime.now();


    public LoginFrame(){
        this.setLayout(null);
        this.init();

        this.setSize(400,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);

    }

    private void init(){
        LovoButton loginButton = new LovoButton("登录",150,180,this);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                login();
            }
        });
    }

    private void login(){
        /*
        创建一个登陆类，书写GUI界面。利用事件要求用户输入用户名和密码，如果登陆成功则写入日志。“张三在  15:32:30 登陆系统。”。
      如果登陆失败，则写入“非法用户张三在  15:32:30 企图登陆系统”
         */
        String time = DateTimeFormatter.ofPattern("HH:mm:ss").format(now);
//        System.out.println(time);
        if (userTxt.getText().equals("张三")&&pwdTxt.getText().equals("123")){
            JOptionPane.showMessageDialog(null,"登陆成功");

            LogObj.writeLog(userTxt.getText()+"  "+time+"  "+"登录系统\n");
        }else {
            JOptionPane.showMessageDialog(null,"登录失败");
            LogObj.writeLog(userTxt.getText()+"  "+time+"  "+"企图登录系统\n");
        }
    }

    public static void main(String[] args) {
        new LoginFrame();
    }
}
